<template>
  <el-dialog :title="title" :visible.sync="dialogShow" @close="closeDialog">
    <el-form
      label-width="120px"
      :model="formData"
      :rules="formRules"
      ref="form"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input style="width: 80%" v-model="formData.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input style="width: 80%" v-model="formData.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getEmployeeData"
        >
          <el-option
            v-for="(item, index) in managerList"
            :key="index"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          style="width: 80%"
          type="textarea"
          :rows="3"
          v-model="formData.introduce"
        />
      </el-form-item>

      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="dialogShow = false">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { getEmployeeSimpleApi } from "@/api/employees";
import {
  getDepartmentListApi,
  addDepartmentApi,
  getDepartmentInfoApi,
  editDepartmentApi,
} from "@/api/department";
export default {
  name: "AddDept",
  data() {
    // 部门名称的检验
    const checkNameRepeat = async (valid, value, cb) => {
      // 找到所有部门名称
      let { depts } = await getDepartmentListApi();
      // 找到当前部门的兄弟部门
      let arr = depts.filter((item) => item.pid === this.formData.pid);
      // 如果是编辑状态下，还要排除自己
      if (this.formData.id) {
        arr = arr.filter((item) => item.id !== this.formData.id);
      }
      // 名称查重
      const isRepeat = arr.some((item) => item.name === value);
      isRepeat ? cb(new Error("名称重复了")) : cb();
    };
    // 部门编码的检验
    const checkCodeRepeat = async (valid, value, cb) => {
      // 找到所有部门名称
      let { depts } = await getDepartmentListApi();
      // 如果是编辑状态下，还要排除自己
      if (this.formData.id) {
        depts = depts.filter((item) => item.id !== this.formData.id);
      }
      // 名称查重
      const isRepeat = depts.some((item) => item.code === value);
      isRepeat ? cb(new Error("编码重复了")) : cb();
    };
    return {
      // 控制弹框
      dialogShow: false,
      // 表单数据
      formData: {
        code: "",
        introduce: "",
        manager: "",
        name: "",
        pid: "",
      },
      // 表单检验规则
      formRules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          { validator: checkNameRepeat, trigger: "blur" },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          { validator: checkCodeRepeat, trigger: "blur" },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
        ],
      },
      managerList: [],
    };
  },
  computed: {
    title() {
      return this.formData.id ? "修改" : "新增";
    },
  },
  methods: {
    // 获取负责人数据
    async getEmployeeData() {
      this.managerList = await getEmployeeSimpleApi();
    },
    // 添加部门
    async btnOk() {
      // 表单数据校验
      console.log(this.$refs.form);
      try {
        await this.$refs.form.validate();
      } catch (error) {
        return console.log(error);
      }
      // 判断是新增还是修改
      if (this.formData.id) {
        await editDepartmentApi(this.formData);
        this.$message.success("修改成功");
      } else {
        await addDepartmentApi(this.formData);
        this.$message.success("添加成功");
      }
      // 更新列表
      this.$emit("updateList");
      // 关闭弹框
      this.dialogShow = false;
    },
    // 关闭弹框
    closeDialog() {
      // 重置表单
      this.formData = {
        code: "",
        introduce: "",
        manager: "",
        name: "",
        pid: "",
      };
      // 移除校验
      this.$refs.form.clearValidate();
    },
    // 回显表单
    async getFormDataInfo(ID) {
      let res = await getDepartmentInfoApi(ID);
      this.formData = res;
      this.dialogShow = true;
    },
  },
};
</script>

<style></style>
